//
//  YCHeroDetail.m
//  LOLBox
//
//  Created by mac on 15-3-30.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCHeroDetail.h"
#import "YCHeroDetailModel.h"
#import "YCHeroQWERModel.h"
#import "YCHeroModel.h"
#import "HttpRequestManager.h"
#import "PublicNavView.h"
#import "PublicAbilities.h"
#import "YCAppDelegate.h"
#import "DataBaseSImple.h"

@interface YCHeroDetail ()
{
    NSString *_currentEnName;
    BOOL _isFav;
    YCHeroModel *_currentModel;
}
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *gold;
@property (weak, nonatomic) IBOutlet UILabel *tags;
@property (weak, nonatomic) IBOutlet UILabel *power;
@property (weak, nonatomic) IBOutlet UIView *NavView;
@property (weak, nonatomic) IBOutlet UIView *Abi;
@property (weak, nonatomic) IBOutlet UIScrollView *AbiContent;
@property (weak, nonatomic) IBOutlet UILabel *abiName;
@property (weak, nonatomic) IBOutlet UILabel *abicontent;
@property (weak, nonatomic) IBOutlet UILabel *abiCooldown;
@property (weak, nonatomic) IBOutlet UILabel *abiRange;
@property (weak, nonatomic) IBOutlet UILabel *abiCost;
@property (weak, nonatomic) IBOutlet UIButton *videoBtn;
@property (weak, nonatomic) IBOutlet UIButton *commonBtn;


@property (weak, nonatomic) IBOutlet UIView *FooterView;


@property (nonatomic, strong) NSMutableArray *abiArr;
@end

@implementation YCHeroDetail

- (void)viewWillAppear:(BOOL)animated
{
    animated = YES;
    YCAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate hideTabBar];
    //self.tabBarController.tabBar.hidden = YES;
}

- (id)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _isFav = NO;
        // Custom initialization
//        CGRect frame = self.AbiContent.frame;
//        frame.size.height = CGRectGetHeight(self.view.frame) - 200;
//        self.AbiContent.frame = frame;
//        
//        
//        NSLog(@"高度  %f",);
        
        [self btaBtn];
        self.abiArr = [NSMutableArray array];
        
        [self createBackBarBtnWithTarget:self WithAction:@selector(popTheView)];
        [self initBgColor];
    }
    return self;
}
- (void)popTheView
{
    if (self.abiArr.count > 0)
    {
        NSLog(@"清数据");
        [self.abiArr removeAllObjects];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//Btn加特技
- (void)btaBtn
{
    //加边框
    CALayer *layer1 = [_commonBtn layer];
    layer1.borderColor = [[UIColor blackColor] CGColor];
    layer1.borderWidth = 1.0f;
    //加边框
    CALayer *layer2 = [_videoBtn layer];
    layer2.borderColor = [[UIColor blackColor] CGColor];
    layer2.borderWidth = 1.0f;
    _commonBtn.layer.cornerRadius = 5;
    _commonBtn.clipsToBounds = YES;
    
    _videoBtn.layer.cornerRadius = 5;
    _videoBtn.clipsToBounds = YES;

}

- (void)fillViewWithModel:(YCHeroDetailModel *)model
{
    self.imgView.image = [UIImage imageNamed:model.name];
    self.name.text = [NSString stringWithFormat:@"%@  %@",model.displayName,model.title];
    self.tags.text = model.tags;
    //self.gold.text = [NSString stringWithFormat:@"金币:%@ 点券:%@",model];
    
}

//从上一个控制器传值过来
- (void)senderModel:(YCHeroModel *)model
{
    
    [self createNavTitleView:model.title];
    
    _currentModel = model;
    _currentEnName = model.enName;
    NSString *enName = model.enName;
   // NSLog(@"英文名字:%@",enName);
    
    //找到当前名字的json文件
    [self getHeroDetail:enName];
    
    [self createAbilities];
    
    [self FavBarBtn];
}

- (void)getHeroDetail:(NSString *)enName
{
    YCHeroDetailModel *model = [[HttpRequestManager shareInstance] getLocalHerosInfoWithEnName:enName];
    
    self.imgView.image = [UIImage imageNamed:model.name];
    self.name.text = [NSString stringWithFormat:@"%@ %@",model.displayName,model.title];
    self.gold.text = [NSString stringWithFormat:@"金币:%@ 点券:%@",model.price1,model.price2];
    self.tags.text = model.tags;
    self.power.text = [NSString stringWithFormat:@"攻:%@ 法:%@ 防:%@ 操作难度:%@",model.ratingAttack,model.ratingMagic,model.ratingDifficulty,model.ratingDifficulty];
    
    YCHeroQWERModel *b = model.b;
    YCHeroQWERModel *q = model.q;
    YCHeroQWERModel *w = model.w;
    YCHeroQWERModel *e = model.e;
    YCHeroQWERModel *r = model.r;
    
    
    [self.abiArr addObject:b];
    [self.abiArr addObject:q];
    [self.abiArr addObject:w];
    [self.abiArr addObject:e];
    [self.abiArr addObject:r];
    
    [self showAbi:b];
}

//算高度
- (CGFloat)setRowHeight:(NSString *)s
{
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGRect frame = [s boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.view.frame) - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil];
    NSLog(@"%f", CGRectGetHeight(frame));
    return CGRectGetHeight(frame);
}


//技能信息
- (void)showAbi:(YCHeroQWERModel *)b
{
    self.abiName.text = b.name;
    self.abicontent.text = b.desc;
    
    if (b.cooldown.length > 0)
    {
        NSLog(@"有冷却");
        self.abiCooldown.hidden = NO;
        self.abiCooldown.text = [NSString stringWithFormat:@"冷却:%@",b.cooldown];
    }
    else
    {
        self.abiCooldown.hidden = YES;
    }
    
    //NSString *bRange = [NSString stringWithFormat:@"%@",b.range];
    if (b.range.length > 0)
    {
        self.abiRange.hidden = NO;
        self.abiRange.text = [NSString stringWithFormat:@"范围:%@",b.range];
    }
    else
    {
        self.abiRange.hidden = YES;
    }
    
    //NSString *bCost = [NSString stringWithFormat:@"%@",b.cost];
    if (b.cost.length > 0)
    {
        self.abiCost.hidden = NO;
        self.abiCost.text = [NSString stringWithFormat:@"消耗:%@",b.cost];
    }
    else
    {
        self.abiCost.hidden = YES;
    }

    CGRect frame = self.abicontent.frame;
    frame.size.height = [self setRowHeight:b.desc];
    self.abicontent.frame = frame;
    
    CGRect CoolFrame = self.FooterView.frame;
    CoolFrame.origin.y = CGRectGetMaxY(self.abicontent.frame) + 15;
    self.FooterView.frame = CoolFrame;
    
    CGFloat hei = CGRectGetMaxY(self.FooterView.frame) + 20;
    self.AbiContent.contentSize = CGSizeMake(self.view.frame.size.width, hei);

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createNavView];
    
    
}

//收藏功能
- (void)FavBarBtn
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 50, 40);
    [btn setImage:[UIImage imageNamed:@"contenttoolbar_hd_fav_light"] forState:UIControlStateNormal];
    btn.tag = 1000;
    [btn setImage:[UIImage imageNamed:@"contenttoolbar_hd_fav_selected_light"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(favOnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    NSArray *arr = [[DataBaseSimple shareInstance] selectFromDBWithHero:_currentEnName];

    if (arr.count > 0) {
        _isFav = YES;
    }
    else
    {
        _isFav= NO;
    }
    btn.selected = _isFav;
    
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtons alloc] createSpaceButtonWithWidth:-10],barBtn];
}

- (void)favOnClick:(UIButton *)btn
{
    NSLog(@"点击判断");
    if (_isFav)
    {
        [[DataBaseSimple shareInstance] deleFromDBWithHero:_currentEnName withIsSuccess:^(BOOL isSuccess) {
            if (isSuccess)
            {
//                NSLog(@"取消收藏成功");
                [self setLoad:@"取消收藏成功"];
                _isFav = NO;
            }
            else
            {
                [self setLoad:@"取消收藏失败"];
                _isFav = YES;
            }
        }];
    }
    else
    {
        [[DataBaseSimple shareInstance] insertIntoDBWithHero:_currentModel withIsSuccess:^(BOOL isSuccess) {
            if (isSuccess)
            {
                [self setLoad:@"添加收藏成功"];
                _isFav = YES;
            }
            else
            {
                [self setLoad:@"添加收藏失败"];
                _isFav = NO;
            }
        }];
    }
    
    btn.selected = _isFav;
}

- (void)setLoad:(NSString *)title
{
    YCLoadingVIew *v = [[YCLoadingVIew alloc] initWithFrame:self.view.bounds withTitle:title];
    [self.view addSubview:v];
    [UIView animateWithDuration:3 animations:^{
        v.alpha = 0;
    } completion:^(BOOL finished) {
        [v removeFromSuperview];
    }];
}

//切换菜单
- (void)createNavView
{
    NSArray *arr1 = @[@"技能",@"介绍",@"出装",@"视频",@"攻略",@"更多"];
    
    //PublicNavView *nav = [[PublicNavView alloc] initWithFrame:self.NavView.frame];
    
    
    PublicNavView *navView = [[PublicNavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    [navView senderValue:arr1 andSecond:arr1];
    [navView creatTheBtnAndIndex:0];
    [navView changValue:@"1" senderBlock:^(NSString *str, NSInteger index) {
        NSLog(@"下标%d",index);
    }];
    
    [self.NavView addSubview:navView];
    
}
//英雄技能
- (void)createAbilities
{
    NSString *b = [NSString stringWithFormat:@"%@_B",_currentEnName];
    NSString *q = [NSString stringWithFormat:@"%@_Q",_currentEnName];
    NSString *w = [NSString stringWithFormat:@"%@_W",_currentEnName];
    NSString *e = [NSString stringWithFormat:@"%@_E",_currentEnName];
    NSString *r = [NSString stringWithFormat:@"%@_R",_currentEnName];
    NSString *more = [NSString stringWithFormat:@"icon_plus"];
    
    NSArray *arr1 = @[b,q,w,e,r,more];
    
    CGFloat weight = self.view.frame.size.width / 6;
    
    PublicAbilities *abi = [[PublicAbilities alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, weight)];
    
    [abi senderValue:arr1 andSecond:arr1];
    [abi creatTheBtnAndIndex:0];
    [abi changValue:@"1" senderBlock:^(NSString *name, NSInteger index) {
        NSLog(@"英雄技能下标%d",index);
        if (index != 5)
        {
            YCHeroQWERModel *m = self.abiArr[index];
            [self showAbi:m];
        }
        
    }];
    
    [self.Abi addSubview:abi];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
